#include <typeinfo>
#include <algorithm>
#include <cassert>
//核心思想:包含父类，通过父类指针指向子类对象，子类对象来存储任意类型的数据
class Any
{
private:
    class holder
    {
    public:
        virtual ~holder() {}
        virtual const std::type_info& type()const = 0;

        virtual holder* clone() const = 0;
    };


    template<class T>
    class placeholder : public holder
    {
    public:
        placeholder(const T& val)
            :_val(val)
        {}

        virtual ~placeholder() = default;

        virtual const std::type_info& type()const override
        {
            return typeid(T);
        }


        virtual holder* clone() const override
        {
            return new placeholder<T>(_val);
        }
    public:
        T _val;
    };
private:
    holder* _content;

public:
    Any()
        :_content(nullptr)
    {}

    ~Any()
    {
        delete _content;
    }


    template<class T>
    Any(const T& val)
    {
        _content = new placeholder<T>(val);
    }


    template <class T>
    T* get()
    {
        assert(typeid(T) == _content->type());
        return &(((placeholder<T>*)_content)->_val);
    }


    const Any& swap(Any& other)
    {
        std::swap(_content, other._content);
        return *this;
    }


    Any(const Any& other)
    {
        if(other._content == nullptr)
        {
            _content = nullptr;
        }
        else
        {
            _content = other._content->clone();
        }
    }


    const Any& operator= (const Any& other)
    {
        if(this == &other)
        {
            return *this;
        }
        Any temp(other);
        swap(temp);
        return *this;
    }


    template<class T>
    const Any& operator= (const T& val)
    {
        Any temp(val);
        swap(temp);
        return *this;
    }

};